﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Model;
using Cn.Cellula.Biz;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web;
using System.Reflection;
using Cn.Edwon.Web.Validation;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Model.Security;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.User {
    public partial class EditFace : PageSupport {
        IUserBiz biz = BizFactory.Get<IUserBiz>();
        String userKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            userKey = (Request.QueryString["id"] == null) ? this.Actor.ID : Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (this.Actor != null) {
                    FillForm();
                }
            }
        }
        private void FillForm() {
            try {
                VSysUserEO user = biz.Load<VSysUserEO>(userKey);
                if (!String.IsNullOrEmpty(user.CoverPath))
                    this.img_Face.ImageUrl = user.CoverPath;
                this.lab_LoginName.Text = user.LoginName;
                this.lab_UserName.Text = user.UserName;
                this.btn_save.Enabled = PowerBiz.UserEdit(this.Actor, user).Editable;
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "装载用户数据时错误");
            }
        }
        private void InitForm() {
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            try {
                TSysUserEO user = biz.Load<TSysUserEO>(userKey);
                if (!String.IsNullOrEmpty(this.file_Face.Value)) {
                    if (String.IsNullOrEmpty(this.hid_CoverFileKey.Value)) {
                        TUploadFileEO f = new TUploadFileEO();
                        ClientBiz.UploadFile(ref f, Request.Files[0], CodeTypeDef.Module.UserFace, user.UserKey, user.OwnerKey, this.Actor);
                        biz.Save<TUploadFileEO>(f);
                    } else {
                        TUploadFileEO feo = ClientBiz.ReUpdateFile(this.hid_CoverFileKey.Value, Request.Files[0], CodeTypeDef.Module.UserFace, user.UserKey, user.OwnerKey, this.Actor);
                        biz.Update<TUploadFileEO>(feo);
                    }
                    BizFactory.Get<ILogBiz>().LogUpdate("头像", user.LoginName, this.Actor, Request);
                } else {
                    SiteInfo.Send(Page, TxtDef.ERROR, "请选择头像图片");
                    return;
                }
            } catch (Exception ex) {
                log.Error("保存头像数据时出现错误:"+ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "保存头像数据时出现错误:"+ex.Message);
                return;
            }
            SiteInfo.SendSucceed(Page, "");
        }

    }
}
